const { Controller } = require('egg');
const {formatDate} = require('../utils/utils');
class rquserController extends Controller {
    async login() {
        try {
            const { ctx } = this;
            var objData = ctx.request.body;
            console.log(objData);
            var rlt = await this.ctx.service.rquser.login(objData.loginname, objData.loginpwd);
            if (rlt) {
                console.log ('rlt',rlt);
                // 生成token
                const token = this.app.jwt.sign({
                    userid: rlt.userid,
                    loginname: rlt.loginname,
                }, this.app.config.jwt.secret,{expiresIn:this.config.jwt.expiresIn});
                console.log ('token',token);

                this.ctx.body = {
                    // code: 20000,
                    // data: rlt,
                    // msg: '登录成功'
                    success: true,
                    data:{
                        userid: rlt.userid,
                        avatar: rlt.headicon,
                        username: rlt.loginname,
                        nickname: rlt.nickname,
                        accessToken: token,
                        roles: ["admin"],
                        // 按钮级别权限
                        permissions: ["*:*:*"],
                        expires: formatDate(Date.now() + 7 * 24 * 60 * 60 * 1000),
                        refreshToken: "eyJhbGciOiJIUzUxMiJ9.adminRefresh",
 
                    }
                }
            }   else {
                this.ctx.body = {
                    code: 500,
                    msg: '服务器login错误'
                }
            }
        } catch (error) {
            this.ctx.body = {
                code: 500,
                msg: '服务器login错误'+error
            }
        }
    }
    async list() {
        try {
            const { ctx } = this;
            const {
                page = 1,    // 当前页码，默认第1页
                limit = 10   // 每页数量，默认10条
            } = ctx.query;
            const offset = (page - 1) * limit; // 计算偏移量
            const result = await ctx.service.rquser.list(offset, limit);
            ctx.body = {
                code: 20000,
                data: result
            }
        } catch (error) {
            this.ctx.body = {
                code: 500,
                msg: '服务器list错误'+error
            }
        }

    }

    async get() {
        try {
            const { ctx } = this;

            var userid = ctx.query.userid;
            if (!userid) {
                ctx.body = {
                    code: 400,
                    msg: '缺少参数'
                }
                return;
            }
            const res = await ctx.service.rquser.detail(userid);
            if (res) {
                ctx.body = {
                    code: 20000,
                    data: res,
                    msg: '查询成功'
                }
            } else {
                ctx.body = {
                    code: 500,
                    msg: '服务器get错误'
                }
            }
        } catch (error) {
            this.ctx.body = {
                code: 500,
                msg: '服务器get错误'+error
            }
        }

    }

    async add() {
        try {
            const { ctx } = this;
            var objData = ctx.request.body;
            var rlt = await this.ctx.service.rquser.add(objData);
            if (rlt) {
                this.ctx.body = {
                    code: 20000,
                    data: rlt,
                    msg: '添加成功'
                }
            } else {
                this.ctx.body = {
                    code: 500,
                    msg: '服务器add错误'
                }
            }
        } catch (error) {
            this.ctx.body = {
                code: 500,
                msg: '服务器add错误'+error
            }
        }



    }

    async del() {
        try {
            const { ctx } = this;
            var userid  = ctx.query.userid;
            console.log('userid',userid)
            const res = await ctx.service.rquser.del(userid);
            if (res.code == 400) {
                ctx.body = {
                    code: 400,
                    msg: res.msg
                }
            } else if (res.code == 200) {
                ctx.body = {
                    code: 20000,
                    msg: res.msg
                }
            } else {
                ctx.body = {
                    code: 500,
                    msg: '服务器del错误'
                }
            }
        } catch (error) {
            this.ctx.body = {
                code: 500,
                msg: '服务器del错误'+error
            }
        }


    }

    async update() {
        try {
            const { ctx } = this;
            var objData = ctx.request.body;
            var rlt = await this.ctx.service.rquser.update(objData);
            if (rlt) {
                if (rlt.code == 400) {
                    this.ctx.body = {
                        code: 400,
                        msg: rlt.msg
                    }
                    return
                }
                this.ctx.body = {
                    code: 20000,
                    data: rlt,
                    msg: '修改成功'
                }
            } else {
                this.ctx.body = {
                    code: 500,
                    msg: '服务器update错误'
                }
            }
        } catch (error) {
            this.ctx.body = {
                code: 500,
                msg: '服务器update错误'+error
            }
        }


    }
}

module.exports = rquserController;
